package com.cxgk.app.cxgkdemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cxgk.app.cxgkdemo.R;
import com.cxgk.app.cxgkdemo.bean.Car;
import com.cxgk.app.cxgkdemo.listener.OnItemChildViewClickListener;
import com.cxgk.app.cxgkdemo.utils.LogUtils;

import java.util.ArrayList;

/**
 * 列表数据适配器
 * Created by lenovo on 2016/12/21.
 */

public class CarListAdapter2 extends RecyclerView.Adapter<CarListAdapter2.ViewHolder> {
    ArrayList<Car> mDatas;
    OnItemChildViewClickListener mOnItemChildViewClickListener;
    private static final int ITEM_EVEN = 0, ITEM_ODD = 1;

    public CarListAdapter2(ArrayList<Car> datas, OnItemChildViewClickListener onItemChildViewClickListener) {
        mDatas = datas;
        mOnItemChildViewClickListener = onItemChildViewClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        return position % 2;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_car, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        LogUtils.d("viewType", getItemViewType(position) + "");
        if (getItemViewType(position) == ITEM_ODD) {
            holder.itemView.setBackgroundResource(R.drawable.select_odd_item);
        } else {
            holder.itemView.setBackgroundResource(R.drawable.select_even_item);
        }
        Car car = mDatas.get(position);
        holder.tv_index.setText((position + 1) + "");
        holder.tv_carType.setText(car.getCarType());
        holder.tv_carNumber.setText(car.getCarNum());
        holder.tv_buyTime.setText(car.getBuyTime());
        holder.tv_carPrice.setText(car.getPrice());
        holder.tv_picName.setText(car.getPictureName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemChildViewClickListener != null) {
                    mOnItemChildViewClickListener.onItemChildViewClickListener(-1, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public Car getItem(int position) {
        return mDatas.get(position);
    }

    /**
     * adapter 更新数据
     *
     * @param datas
     */
    public void refreshData(ArrayList<Car> datas) {
        mDatas = datas;
        notifyDataSetChanged();

    }

    /**
     * adapter 追加数据
     *
     * @param datas
     */
    public void appendData(ArrayList<Car> datas) {
        for (int i = 0; i < datas.size(); i++) {
            this.mDatas.add(datas.get(i));
            notifyItemChanged(mDatas.size() + i);
        }
        //        this.mDatas.addAll(datas);
        //        notifyDataSetChanged();
    }

    /**
     * 移除数据
     *
     * @param position
     */
    public void removeItem(int position) {
        this.mDatas.remove(position);
        this.notifyItemRemoved(position);
        this.notifyItemRangeChanged(position, mDatas.size());
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            tv_index = (TextView) itemView.findViewById(R.id.tv_index);
            tv_carType = (TextView) itemView.findViewById(R.id.tv_carType);
            tv_carNumber = (TextView) itemView.findViewById(R.id.tv_carNumber);
            tv_buyTime = (TextView) itemView.findViewById(R.id.tv_buyTime);
            tv_carPrice = (TextView) itemView.findViewById(R.id.tv_carPrice);
            tv_picName = (TextView) itemView.findViewById(R.id.tv_picName);
        }

        TextView tv_index, tv_carType, tv_carNumber, tv_buyTime, tv_carPrice, tv_picName;
    }

}
